/**
 * 海康用户hkhkUser.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    $("#hkUser_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        striped:true,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/hkUser/hkUserList.do',
        idField: 'userUuid',
        frozenColumns:[[
            {field:'userUuid',checkbox:true}
        ]],
        columns: [
            [
                {field: 'loginAccount', title: '登录用户名', width: 150, fixed: true},
                {field: 'userName', title: '用户名', width: 150, fixed: true},
                {field: 'level', title: '用户级别', width: 80, fixed: true},
                {field: 'locked', title: '锁定状态', width: 80, align:'center',fixed: true,
                    formatter: function (val) {
                        if (val == 0) return "<span style='color: green; font-weight: bold'>正常</span>";
                        if (val == 1) return "<span style='color: red; font-weight: bold'>锁定</span>";
                    }
                },
                {field: 'endTime', title: '失效日期', width: 120},
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#hkUser_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#hkUser_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#hkUser_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#hkUser_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    var isclick= true;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            hkUser_findList();
                        }
                    });
                }
                //同步用户
                if("synchronizBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            if(isclick) {
                                isclick = false;
                                $.ajax({
                                    url: CONTEXT_PATH + "/hkUser/hkUserSynchronizd.do",
                                    type: "get",
                                    success: function (data) {
                                        isclick = true;
                                        messager(data);
                                    }
                                });
                            }else{
                                $.messager.show({ title:"系统提示", msg: "不能频繁同步数据", timeout:3000, showType:"slide" });
                            }
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            hkUser_veiw($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查看海康用户详情
 */
function hkUser_veiw(iconCls){
    var checked = $("#hkUser_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要查看的用户", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#hkUser_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(hkxq.id, hkxq.url + row.userUuid, iconCls, hkxq.closable, hkxq.title);
}


/**
 * 查询海康用户
 */
function hkUser_findList() {
    //刷新dg页码
    refreshPageNumber("hkUser_dataList");
    $("#hkUser_dataList").datagrid("reload", {
        loginAccount: $("#loginAccount_").textbox("getValue"),
        userName: $("#userName_").textbox("getValue")
    });
    $("#hkUser_dataList").datagrid("clearSelections");

}





